/*
 * Created Jul 7, 2010
 */
package ltg.ps.phenomena.zooroom;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import ltg.ps.api.phenomena.PushPhenomena;

/**
 * This class represents ZooRoom which is so far simply a collection of tracking data.
 *
 * @author Gugo
 */
public class ZooRoom extends PushPhenomena {

	// Tracking data
	private static final String FILES_DIR = "src/main/resources/";
	private List<Player> data = null;


	/**
	 * @param instanceName
	 */
	public ZooRoom(String instanceName) {
		super(instanceName);
		data = new ArrayList<Player>();
		loadData();
		log.info("Done loading data. Ready to start.");
	}

	/* (non-Javadoc)
	 * @see ltg.ps.model.Phenomena#update()
	 */
	@Override
	protected void update() {
		// do nothing... so far...
	}
	
	
	public Player getPlayerCurrentState(int player) {
		return data.get(player);
	}
	
	
	public List<Record> getPlayerUpdates(int player) {
		// TODO write this function!
		return null;
	}



	private void loadData() {
		File dir = new File(FILES_DIR);
		FilterFiles only = new FilterFiles("player_", "csv");
		String[] playerFiles = dir.list(only);
		for (int i = 0; i<playerFiles.length; i++) {
			String fileName = FILES_DIR + playerFiles[i];
			loadPlayer(fileName);
		}
	}

	
	/**
	 * Stores data for a single player
	 *
	 * @param file
	 */
	private void loadPlayer(String file) {
		
		// Open data file
		BufferedReader fin = null;
		try {
			fin = new BufferedReader(new FileReader(new File(file)));
		} catch (FileNotFoundException e) {
			log.error("Unable to open data file!");
		}
		
		// Parse data
		Player player = new Player();
		DateFormat dFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String delimiters = "[,\\s\\t]+";
		Calendar calendar = Calendar.getInstance();
		String s = null;
		String[] currentRecord = null;
		String dateAndTime = "";
		try {
			while ((s = fin.readLine()) != null) {
				currentRecord = s.split(delimiters);
				dateAndTime = currentRecord[0];
				dateAndTime = dateAndTime + " " + currentRecord[1];
				Date currentDateRecord = null;
				try {
					currentDateRecord = dFormat.parse(dateAndTime);
				} catch (ParseException e) {
					continue;
				}
				calendar.setTime(currentDateRecord);
				int timeInSeconds = calendar.get(Calendar.HOUR_OF_DAY)*60*60 + calendar.get(Calendar.MINUTE)*60 + calendar.get(Calendar.SECOND);
				float lat = Float.parseFloat(currentRecord[2]);
				float lon = Float.parseFloat(currentRecord[3]);

				player.setRecord(timeInSeconds, lat, lon);

			}
		} catch (IOException e1) {
			log.error("Unable to read data from file");
		} 

		// Close data file
		try {
			fin.close();
		} catch (IOException e) {
			log.warn("Unable to close data file");
		}
		
		// Add player to data array
		data.add(player);
	}


	/**
	 * Searches for the files that store the GPS data 
	 */
	private class FilterFiles implements FilenameFilter {
		String ext;
		String start;
		public FilterFiles(String start, String ext) {
			this.ext = "." + ext;
			this.start = start;
		}
		public boolean accept(File dir, String name) {
			return (name.endsWith(ext) && name.startsWith(start));
		}
	}


	@Override
	public void configure(String configXML) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void restore() {
		// TODO Auto-generated method stub
		
	}
}
